package com.fastCms.business.bim.manage.impl;

import com.fastCms.business.bim.daos.DeviceGroupMapper;
import com.fastCms.business.bim.daos.DeviceMapper;
import com.fastCms.business.bim.daos.FiledsMapper;
import com.fastCms.business.bim.manage.VideoManageService;
import com.fastCms.business.bim.pojo.po.DeviceGroupPO;
import com.fastCms.business.bim.pojo.vo.*;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Component
@Slf4j
public class VideoManageServiceImpl implements VideoManageService {

    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    @Autowired
    private FiledsMapper filedsMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public ApiResponse<List<DeviceGroupAndDevicesVO>> list() {
        DeviceGroupPO deviceGroupPO = new DeviceGroupPO();
        deviceGroupPO.setGroupType("VIDEO");
        List<DeviceGroupVO> list = deviceGroupMapper.showList(deviceGroupPO);
        for (DeviceGroupVO group : list) {
            List<DeviceInfoVO> deviceInfos = deviceGroupMapper.getDevicesByGroupId(group.getId());
            for (DeviceInfoVO device : deviceInfos) {
                List<DeviceFiledsValueVO> filedValues = filedsMapper.getDeviceFieldsValue(device.getDeviceTypeId(), device.getId());
                device.setFiledsValueList(filedValues);
            }
            group.setDeviceList(deviceInfos);
        }
        return ResponseUtil.success(list);
    }

    @Override
    public ApiResponse<List<DeviceInfoVO>> getVideos(Integer groupId) {
        List<DeviceInfoVO> deviceInfos = deviceGroupMapper.getDevicesByGroupId(groupId);
        for (DeviceInfoVO device : deviceInfos) {
            List<DeviceFiledsValueVO> filedValues = filedsMapper.getDeviceFieldsValue(device.getDeviceTypeId(), device.getId());
            device.setFiledsValueList(filedValues);
        }
        return ResponseUtil.success(deviceInfos);
    }

    @Override
    public ApiResponse<List<DeviceInfoVO>> showAllVideo() {
        List<DeviceInfoVO> videoList = deviceMapper.getVideoList(20);
        for (DeviceInfoVO device : videoList) {
            List<DeviceFiledsValueVO> filedValues = filedsMapper.getDeviceFieldsValue(device.getDeviceTypeId(), device.getId());
            device.setFiledsValueList(filedValues);
        }
        return ResponseUtil.success(videoList);
    }

}
